package com.example.stars_project_android.personalCenter;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.example.stars_project_android.MainActivity;
import com.example.stars_project_android.R;
import com.example.stars_project_android.index.Index;

/**
 * 设置界面
 */
public class SetUp extends AppCompatActivity implements View.OnClickListener {

    private Switch aSwitch, swCollection, swNotice;   //日间夜间模式切换,不展示收藏内容，新消息通知
    private ImageButton imageButton;  //返回个人中心
    private LinearLayout idSafe, blackUser, about;  //账号与安全，黑名单设置，关于
    private Button logOut;  //退出账号
    private ActionBar supportActionBar;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up);
        supportActionBar = getSupportActionBar();
        setView();
        initData();

        //日夜模式切换
        preferences = getSharedPreferences("appcompat", Context.MODE_PRIVATE);
        //取出保存的值
        boolean isChecked = preferences.getBoolean("isChecked",false);
        //根据保存的值设置主题状态
        if (isChecked) {
            getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            //设置夜间模式背景色
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setNavigationBarColor(Color.parseColor("#303030"));
            }
        } else {
            getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            //设置日间模式背景
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setNavigationBarColor(Color.parseColor("#ffffff"));
            }
        }


        //点击不显示收藏按钮
        // 添加监听
        swCollection.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //不显示收藏

                    Log.e("msg","不显示收藏");
                }else {
                    //显示收藏

                    Log.e("msg","显示收藏");
                }
            }
        });

        //点击新消息通知按钮
        // 添加监听
        swNotice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //新消息通知

                    Log.e("msg","新消息通知");
                }else {
                    //新消息不通知

                    Log.e("msg","新消息不通知");
                }
            }
        });

    }

    /**
     * 日夜模式切换
     */
    @SuppressLint("ResourceAsColor")
    public void initData() {
        //取出保存的数据
        preferences = getSharedPreferences("appcompat",Context.MODE_PRIVATE);
        editor = preferences.edit();

        //获取状态并设置当前状态
        boolean isChecked = preferences.getBoolean("isChecked",false);
        aSwitch.setChecked(isChecked);

        //点击日夜模式切换按钮
        // 添加监听
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
//                  //setLocalNightMode()只对当前页面有效果
//                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    //setDefaultNightMode()对所有页面有效果
                    getDelegate().setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    recreate();
                    editor.putBoolean("isChecked",true);
                    editor.commit();
                }else {
//                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    getDelegate().setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    recreate();
                    editor.putBoolean("isChecked",false);
                    editor.commit();
                }
            }
        });
    }

    private void setView() {
        imageButton = findViewById(R.id.btn_set_return);
        imageButton.setOnClickListener(this);
        aSwitch = findViewById(R.id.sw_night);
        swCollection = findViewById(R.id.sw_collection);
        swNotice = findViewById(R.id.sw_notice);
        idSafe = findViewById(R.id.id_safe);
        idSafe.setOnClickListener(this);
        blackUser = findViewById(R.id.black_user);
        blackUser.setOnClickListener(this);
        about = findViewById(R.id.about);
        about.setOnClickListener(this);
        logOut = findViewById(R.id.btn_logout);
        logOut.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击返回按钮
            case R.id.btn_set_return:
                Intent intent5 = new Intent(this, Index.class);
                intent5.putExtra("index",4);
                startActivity(intent5);
                break;
            //点击账号与安全
            case R.id.id_safe:
                Intent intent = new Intent(this,IdSafe.class);
                startActivity(intent);
                break;
            //点击黑名单设置
            case R.id.black_user:
                Intent intent1 = new Intent(this,BlackUser.class);
                startActivity(intent1);
                break;
            //点击关于星记
            case R.id.about:
                Intent intent2 = new Intent(this,About.class);
                startActivity(intent2);
                break;
            //点击退出账号
            case R.id.btn_logout:
                Intent intent4 = new Intent(this, MainActivity.class);

                startActivity(intent4);
                break;
            default:
                break;
        }
    }

}